package com.chen.authority.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.PageResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.util.ExcelUtil;
import com.chen.authority.common.util.WrapperUtil;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.model.qo.LoginLogQo;
import com.chen.authority.model.po.LoginLogPo;
import com.chen.authority.service.LoginLogService;

import javax.servlet.http.HttpServletResponse;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;

/**
 * 登录日志管理
 *
 * @author chengy
 * @date 2022/11/19
 */
@Api(tags = "登录日志管理 LoginLogController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/login-log")
public class LoginLogController {

    private final LoginLogService loginLogService;

    /**
     * 删除登录日志
     *
     * @param ids 日志id数组
     * @return Response
     */
    @ApiOperation("删除登录日志")
    @SaCheckPermission("system:login-log:delete")
    @Log(module = "系统管理-登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Response delete(@PathVariable Integer[] ids) {
        return loginLogService.removeByIds(Arrays.asList(ids)) ? Response.success() : Response.fail();
    }

    /**
     * 清空登录日志
     *
     * @return Response
     */
    @ApiOperation("清空登录日志")
    @SaCheckPermission("system:login-log:delete")
    @Log(module = "系统管理-登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping
    public Response delete() {
        return loginLogService.remove(Wrappers.emptyWrapper()) ? Response.success() : Response.fail();
    }

    /**
     * 查询登录日志列表
     *
     * @param pageQo     分页数据
     * @param loginLogQo 查询参数
     * @return PageResponse<LoginLog>
     */
    @ApiOperation("查询登录日志列表")
    @SaCheckPermission("system:login-log:list")
    @GetMapping("/list")
    public PageResponse<LoginLogPo> list(PageQo pageQo, LoginLogQo loginLogQo) {
        return PageResponse.success(loginLogService.page(pageQo.build(), WrapperUtil.buildQueryWrapper(loginLogQo)));
    }

    /**
     * 导出登录日志列表
     *
     * @param response 响应
     * @param qo      查询参数
     */
    @ApiOperation("导出登录日志列表")
    @SaCheckPermission("system:login-log:export")
    @Log(module = "系统管理-登录日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoginLogQo qo) {
        ExcelUtil.exportExcel(response, "登录日志数据", LoginLogPo.class, loginLogService.list(WrapperUtil.buildQueryWrapper(qo)));
    }

}